import sys, os
import tkMessageBox as msgbox
from string import *
wordlist = {}
once = 0
more = 0
curtop = 0
curtopword = ""
for word in split(open(sys.argv[1]).read()):
    word = word.translate(maketrans("",""), punctuation)
    word = lower(word)
    if word not in wordlist:
        wordlist[word] = 1
    else:
        wordlist[word] += 1
for word in wordlist:
    if wordlist[word] == 1:
        once += 1
    if wordlist[word] != 1:
        more += 1
x = sys.argv[1] + ".papanalyze.csv"
x2 = sys.argv[1] + ".papanalyze.txt"
f = open(x, "w")
f2 = open(x2, "w")
f.write("Word,TimesUsed")
for word in wordlist:
    f.write(word + "," +str(wordlist[word]) + os.linesep)
    f2.write(word + " used " + str(wordlist[word]) + " times." + os.linesep)
for word in wordlist:
    if wordlist[word] > curtop:
        curtopword = word
        curtop = wordlist[word]
f2.write("Word used most: " + curtopword + os.linesep)
f2.write("Vocabulary diversity score: " + str(len(wordlist)/10) + os.linesep)
f.close()
f2.close()
msgbox.showinfo("", "Words used once: " + str(once) + os.linesep + \
                "Words used more than once: " + str(more) + os.linesep + \
                "Vocabulary Diversity Score: " + str(len(wordlist)/10) + os.linesep + \
                "Most Used Word: " + curtopword + os.linesep + \
                "A complete report can be found at:" + os.linesep + \
                x + os.linesep + x2)
